package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断是否存在
        List<ShoppingCart> list =  shoppingCartMapper.list(shoppingCart);
        //存在update
        if (list.size() > 0 && list != null){
            ShoppingCart sc = list.get(0);
            sc.setNumber(sc.getNumber()+1);
            shoppingCartMapper.updateNumberById(sc);
        }else{
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if(dishId != null){
                DishVO dishById = dishMapper.getDishById(dishId);
                shoppingCart.setName(dishById.getName());
                shoppingCart.setImage(dishById.getImage());
                shoppingCart.setAmount(dishById.getPrice());
            }else{
                SetmealVO setmealById = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setName(setmealById.getName());
                shoppingCart.setImage(setmealById.getImage());
                shoppingCart.setAmount(setmealById.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }

        //不存在insert
    }

    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list = shoppingCartMapper.listById(BaseContext.getCurrentId());
        return list;
    }

    @Override
    public void cleanAll() {
        shoppingCartMapper.cleanAll(BaseContext.getCurrentId());
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart sc = list.get(0);
        if(sc.getNumber() > 1){
            sc.setNumber(sc.getNumber()-1);
            shoppingCartMapper.updateNumberById(sc);
        }else{
            shoppingCartMapper.deleteOne(shoppingCartDTO);
        }


    }
}
